<?php

namespace app\controller;

use app\service\WorkTimeService;
use support\Request;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class WorkTimeController
{

    /**
     * @var
     */
    private $workTimeService;

    public function __construct()
    {
        $this->workTimeService = WorkTimeService::getInstance();
    }


    /**
     * 工时报表
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getTenantUserStatistics(Request $request): Response
    {
        $param = $request->post();
        $tenantId = $param['tenant_id'] ?? $request->getTenantId();
        $res = $this->workTimeService->getTenantUserStatistics($tenantId, $param['project_id_list'] ?? [], $param['work_date_from'] ?? '', $param['work_date_to'] ?? '', $param['department_id'] ?? 0, $param['role_code'] ?? '', $param['user_ids'] ?? []);
        return json(success_response('', $res));
    }

    /**
     * 导出工时报表
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function exportUserStatistics(Request $request): Response
    {
        $param = $request->post()['param'];

        $projectIdList = param_to_array($param['project_id_list'] ?? '', ',', 'intval');
        $workDateStart = $param['work_date_from'] ?? '';
        $workDateEnd = $param['work_date_to'] ?? '';
        $page = param_to_array($param['page'] ?? '', ',', 'intval');

        $res = $this->workTimeService->exportStatistics($request, $projectIdList, $workDateStart, $workDateEnd, $page);
        return json(success_response('', $res));
    }

    /**
     * 查询用户填写的工时汇总
     * @param Request $request
     * @return Response
     */
    public function getUserWorkTimes(Request $request)
    {
        $param = $request->all();
        $data = $this->workTimeService->getUserWorkTimes($param, $request->getTenantId());
        return json(success_response('success', $data));
    }
}
